package co.fitstart.entity.report;

import java.beans.Transient;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.demo2do.core.format.DecimalFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.course.Course;
import co.fitstart.entity.course.CourseUnit;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author lidan
 *
 */
@Entity
@Table(name = "revenue_report")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class RevenueReport {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 营收类型 */
    private RevenueType revenueType;
    
    /** 金额（分） */
    @DecimalFormat
    private int offer;
    
    /** 学员名称 */
    private String studentName;
    
    /** 学员电话 */
    private String studentMobile;
    
    /** 教练姓名 */
    private String coachName;
    
    /** 销售人员 */
    private String salesman;
    
    /** 购买/退款时间 */
    private Date occurTime;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Course course;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Site site;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private CourseRound courseRound;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public RevenueReport() {
        
    }
    
    public int getUnitPricePosition() {
        
        if(this.course.getCourseUnit().equals(CourseUnit.NORMAL_INDIVIDUAL) ||
                this.course.getCourseUnit().equals(CourseUnit.OTHER_INDIVIDUAL) ||
                    this.course.getCourseUnit().equals(CourseUnit.NEW_OTHER_INDIVIDUAL)) {
            return 1;
        } else if (this.course.getCourseUnit().equals(CourseUnit.FAST_INDIVIDUAL)) {
            return 2;
        } else if (this.course.getCourseUnit().equals(CourseUnit.SHAPE_INDIVIDUAL)) {
            return 3;
        } else if (this.course.getCourseUnit().equals(CourseUnit.Y_BOXING_SPECIAL) ||
                this.course.getCourseUnit().equals(CourseUnit.BOXING_SPECIAL)) {
            return 4;
        } else if (this.course.getCourseUnit().equals(CourseUnit.H_STRETCH_SPECIAL)) {
            return 5;
        } else if (this.course.getCourseUnit().equals(CourseUnit.BABY_SPECIAL)) {
            return 6;
        } else if (this.course.getCourseUnit().equals(CourseUnit.BODY_SPECIAL)) {
            return 7;
        } else if (this.course.getCourseUnit().equals(CourseUnit.NORMAL_POSTURE) || 
                this.course.getCourseUnit().equals(CourseUnit.POSTURE_BEAUTY)) {
            return 8;
        } else if (this.course.getCourseUnit().equals(CourseUnit.POSTURE_FIX)) {
            return 9;
        } else if (this.course.getCourseUnit().equals(CourseUnit.YEAR_ROUND)) {
            return 10;
        } else if (this.course.getCourseUnit().equals(CourseUnit.MONTH_ROUND)) {
            return 11;
        } else if (this.course.getCourseUnit().equals(CourseUnit.SEASON_ROUND)) {
            return 12;
        } else if (this.course.getCourseUnit().equals(CourseUnit.YEAR_SPECIAL_ROUND)) {
            return 13;
        } else if (this.course.getCourseUnit().equals(CourseUnit.MONTH_SPECIAL_ROUND)) {
            return 14;
        } else if (this.course.getCourseUnit().equals(CourseUnit.SEASON_SPECIAL_ROUND)) {
            return 15;
        } else if (this.course.getCourseUnit().equals(CourseUnit.Y_HANG_TRAINING)) {
            return 31;
        } else if (this.course.getCourseUnit().equals(CourseUnit.LING_HU_SPECIAL)) {
            return 33;
        } else if (this.course.getCourseUnit().equals(CourseUnit.VIPR_SPECIAL)) {
            return 35;
        }  else if (this.course.getCourseUnit().equals(CourseUnit.YOUNG_INDIVIDUAL)) {
            return 37;
        } else {
            return 0;
        }
        
    }
    
    public int getUnitAmountPosition() {
        
        if(this.course.getCourseUnit().equals(CourseUnit.NORMAL_INDIVIDUAL) ||
                this.course.getCourseUnit().equals(CourseUnit.OTHER_INDIVIDUAL) ||
                    this.course.getCourseUnit().equals(CourseUnit.NEW_OTHER_INDIVIDUAL)) {
            return 16;
        } else if (this.course.getCourseUnit().equals(CourseUnit.FAST_INDIVIDUAL)) {
            return 17;
        } else if (this.course.getCourseUnit().equals(CourseUnit.SHAPE_INDIVIDUAL)) {
            return 18;
        } else if (this.course.getCourseUnit().equals(CourseUnit.Y_BOXING_SPECIAL) ||
                this.course.getCourseUnit().equals(CourseUnit.BOXING_SPECIAL)) {
            return 19;
        } else if (this.course.getCourseUnit().equals(CourseUnit.H_STRETCH_SPECIAL)) {
            return 20;
        } else if (this.course.getCourseUnit().equals(CourseUnit.BABY_SPECIAL)) {
            return 21;
        } else if (this.course.getCourseUnit().equals(CourseUnit.BODY_SPECIAL)) {
            return 22;
        } else if (this.course.getCourseUnit().equals(CourseUnit.NORMAL_POSTURE) || 
                this.course.getCourseUnit().equals(CourseUnit.POSTURE_BEAUTY)) {
            return 23;
        } else if (this.course.getCourseUnit().equals(CourseUnit.POSTURE_FIX)) {
            return 24;
        } else if (this.course.getCourseUnit().equals(CourseUnit.YEAR_ROUND)) {
            return 25;
        } else if (this.course.getCourseUnit().equals(CourseUnit.MONTH_ROUND)) {
            return 26;
        } else if (this.course.getCourseUnit().equals(CourseUnit.SEASON_ROUND)) {
            return 27;
        } else if (this.course.getCourseUnit().equals(CourseUnit.YEAR_SPECIAL_ROUND)) {
            return 28;
        } else if (this.course.getCourseUnit().equals(CourseUnit.MONTH_SPECIAL_ROUND)) {
            return 29;
        } else if (this.course.getCourseUnit().equals(CourseUnit.SEASON_SPECIAL_ROUND)) {
            return 30;
        } else if (this.course.getCourseUnit().equals(CourseUnit.Y_HANG_TRAINING)) {
            return 32;
        } else if (this.course.getCourseUnit().equals(CourseUnit.LING_HU_SPECIAL)) {
            return 34;
        } else if (this.course.getCourseUnit().equals(CourseUnit.VIPR_SPECIAL)) {
            return 36;
        } else if (this.course.getCourseUnit().equals(CourseUnit.YOUNG_INDIVIDUAL)) {
            return 38;
        } else {
            return 0;
        }
        
    }
    
    /**
     * 
     * @param saleman
     * @return
     */
    public RevenueReport changeSaleman(String saleman) {
    	
    	this.salesman = saleman;
    	return this;
    }
    
    /**
     * 
     * @param userName
     * @return
     */
    public RevenueReport changeUsername(String userName) {
    	
    	this.studentName = userName;
    	
    	return this;
    }
    
    /**
     * 
     * @param mobile
     * @return
     */
    public RevenueReport changeUserMobile(String mobile) {
    	
    	this.studentMobile = mobile;
    	
    	return this;
    }
    
    public RevenueReport changePrice(int price) {
    	
    	this.offer = price;
    	
    	return this;
    }
    
    /**
     * 
     * @param order
     * @param courseRound
     * @param revenueType
     */
    public RevenueReport(Order order, CourseRound courseRound, RevenueType revenueType) {
        this.revenueType = revenueType;
        this.offer = revenueType.isPurchase() ? order.getPrice() : order.getRefundPrice();
        this.courseRound = courseRound;
        this.studentName = courseRound.getStudent().getName();
        this.studentMobile = courseRound.getStudent().getMobile();
        if(courseRound.getCoach() != null) {
            this.coachName = courseRound.getCoach().getName();
        }
        this.occurTime = new Date();
        this.course = courseRound.getCourse();
        this.site = courseRound.getSite();
        this.disabled = false;
    }
    
    /**
     * 
     * @param offer
     * @return
     */
    public RevenueReport editOffer(int offer) {
        this.offer = offer;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public RevenueReport disable() {
        this.disabled = true;
        return this;
    }

    /**
     * 
     * @return
     */
    @Transient
    public int getSignedCourseAmount() {
        if(revenueType.isPurchase()) {
            if(courseRound.getCourseType().isYear()) {
                if(courseRound.getCourseUnit().equals(CourseUnit.YEAR_ROUND) || courseRound.getCourseUnit().equals(CourseUnit.YEAR_SPECIAL_ROUND)) {
                	return 365;
                } else if(courseRound.getCourseUnit().equals(CourseUnit.SEASON_ROUND) || courseRound.getCourseUnit().equals(CourseUnit.SEASON_SPECIAL_ROUND)) {
                	return 90;
                } else if(courseRound.getCourseUnit().equals(CourseUnit.MONTH_ROUND) || courseRound.getCourseUnit().equals(CourseUnit.MONTH_SPECIAL_ROUND)) {
                	return 90;
                } else {
                	return 365;
                }
            } else {
                if(courseRound.getAttrs() != null && courseRound.getAttrs().get("course_amount") != null) {
                    return Integer.parseInt(courseRound.getAttrs().get("course_amount").getValue());
                } else {
                    return course.getHours();
                }
            }
        } else {
            if(courseRound.getCourseType().isYear()) {
            	if(courseRound.getCourseUnit().equals(CourseUnit.YEAR_ROUND) || courseRound.getCourseUnit().equals(CourseUnit.YEAR_SPECIAL_ROUND)) {
                	return -365;
                } else if(courseRound.getCourseUnit().equals(CourseUnit.SEASON_ROUND) || courseRound.getCourseUnit().equals(CourseUnit.SEASON_SPECIAL_ROUND)) {
                	return -90;
                } else if(courseRound.getCourseUnit().equals(CourseUnit.MONTH_ROUND) || courseRound.getCourseUnit().equals(CourseUnit.MONTH_SPECIAL_ROUND)) {
                	return -90;
                } else {
                	return -365;
                }
            } else {
                if(courseRound.getAttrs() != null && courseRound.getAttrs().get("course_amount") != null) {
                    return -Integer.parseInt(courseRound.getAttrs().get("course_amount").getValue());
                } else {
                    return -course.getHours();
                }
            }
        }
    }

    /**
     * 
     * @return
     */
    @Transient
    public int getSignedOffer() {
        if(revenueType.isPurchase()) {
            return offer;
        } else {
            return -offer;
        }
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the revenueType
     */
    public RevenueType getRevenueType() {
        return revenueType;
    }

    /**
     * @return the offer
     */
    public int getOffer() {
        return offer;
    }

    /**
     * @return the studentName
     */
    public String getStudentName() {
        return studentName;
    }

    /**
     * @return the studentMobile
     */
    public String getStudentMobile() {
        return studentMobile;
    }

    /**
     * @return the coachName
     */
    public String getCoachName() {
        return coachName;
    }

    /**
     * @return the salesman
     */
    public String getSalesman() {
        return salesman;
    }

    /**
     * @return the occurTime
     */
    public Date getOccurTime() {
        return occurTime;
    }

    /**
     * @return the course
     */
    public Course getCourse() {
        return course;
    }

    /**
     * @return the site
     */
    public Site getSite() {
        return site;
    }

    /**
     * @return the courseRound
     */
    public CourseRound getCourseRound() {
        return courseRound;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param revenueType the revenueType to set
     */
    public void setRevenueType(RevenueType revenueType) {
        this.revenueType = revenueType;
    }

    /**
     * @param offer the offer to set
     */
    public void setOffer(int offer) {
        this.offer = offer;
    }

    /**
     * @param studentName the studentName to set
     */
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    /**
     * @param studentMobile the studentMobile to set
     */
    public void setStudentMobile(String studentMobile) {
        this.studentMobile = studentMobile;
    }

    /**
     * @param coachName the coachName to set
     */
    public void setCoachName(String coachName) {
        this.coachName = coachName;
    }

    /**
     * @param salesman the salesman to set
     */
    public void setSalesman(String salesman) {
        this.salesman = salesman;
    }

    /**
     * @param occurTime the occurTime to set
     */
    public void setOccurTime(Date occurTime) {
        this.occurTime = occurTime;
    }

    /**
     * @param course the course to set
     */
    public void setCourse(Course course) {
        this.course = course;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Site site) {
        this.site = site;
    }

    /**
     * @param courseRound the courseRound to set
     */
    public void setCourseRound(CourseRound courseRound) {
        this.courseRound = courseRound;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
}
